from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect,HttpResponse
from dbadmin import models
class M1(MiddlewareMixin):
    def process_request(self,request):
        user_name = request.session.get("username",0)
        if request.path == "/":
            if user_name == 0:

                return redirect("/user/login/")
            else:
                return redirect("/user/index/")
        elif user_name == 0 and request.path != "/user/login/":
            # print(request.path)
            return redirect("/user/login/")

        elif user_name == 0 and request.path == "/user/login/":
            pass
        else:
            user_url_tmp = models.users.objects.filter(user_name=user_name).values("user_permission__permission_url")
            user_admin = models.users.objects.filter(user_name=user_name).values("user_admin")[0]["user_admin"]

            if user_admin == "1":
                pass
            else:
                user_url = []
                for i in user_url_tmp:
                    user_url.append(i["user_permission__permission_url"])
                if request.path == "/user/index/":
                    pass
                elif request.path == "/user/loginout/":
                    pass
                elif request.path == "/user/login/":
                    pass
                elif request.path not in user_url:
                    return HttpResponse("该账号无权限访问，请联系管理员")


